package api

import (
	"longmen/server/pkg/common/api_models"
	string2 "longmen/server/pkg/common/helper/string"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql/models/models_main"

	"github.com/labstack/echo/v4"
)

// @Summary 获取抽奖活动信息
// @Description 获取抽奖活动信息
// @Accept  json
// @Produce  json
// @Tags Lottery
// @Success 200 {object} api_models.GetLotteryResp "result"
// @Router /api/lottery/getLottery [POST]
func (base *Controller) GetLottery(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		user = &models_main.User{Id: 0}
	}
	resp, err := base.Service.GetLottery(user.Id)
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 获取中奖弹幕
// @Description 获取中奖弹幕
// @Accept  json
// @Produce  json
// @Tags Lottery
// @Success 200 {object} []string "result"
// @Router /api/lottery/getAwardBullets [POST]
func (base *Controller) GetAwardBullets(c echo.Context) error {
	resp, err := base.Service.GetAwardBullets()
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 获取奖品信息
// @Description 获取奖品信息
// @Accept  json
// @Produce  json
// @Tags Lottery
// @Param   sendBody body api_models.LotteryAwardsInfoReq true "body"
// @Success 200 {object} api_models.LotteryAwardsInfoResp "result"
// @Router /api/lottery/getLotteryAwardsInfo [POST]
func (base *Controller) GetLotteryAwardsInfo(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		user = &models_main.User{Id: 0}
	}

	req := &api_models.LotteryAwardsInfoReq{}
	if err = util.Json2Struct(c.Get("body").(string), req); err != nil {
		return util.BuildFail(c, 1, "", err)
	}

	resp, err := base.Service.GetLotteryAwardsInfo(user.Id, req.LottId)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 点击抽奖
// @Description 点击抽奖
// @Accept  json
// @Produce  json
// @Tags Lottery
// @Param   sendBody body api_models.DrawLotteryReq true "body"
// @Success 200 {object} api_models.DrawLotteryResp "result"
// @Router /api/lottery/drawLottery [POST]
func (base *Controller) DrawLottery(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}

	req := &api_models.DrawLotteryReq{}
	if err = util.Json2Struct(c.Get("body").(string), req); err != nil {
		return util.BuildFail(c, 1, "", err)
	}

	resp, err := base.Service.DrawLottery(user, *req)
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 获取抽奖历史
// @Description 获取抽奖历史
// @Accept  json
// @Produce  json
// @Tags Lottery
// @Success 200 {object} api_models.LotteryHistoryResp "result"
// @Router /api/lottery/getLotteryHistory [POST]
func (base *Controller) GetLotteryHistory(c echo.Context) error {
	req := &api_models.LotteryHistoryReq{}
	if err := util.Json2Struct(c.Get("body").(string), req); err != nil {
		return util.BadParam(c, err)
	}
	req.Page = string2.GetDefaultNum(req.Page, 1)
	req.Size = string2.GetDefaultNum(req.Size, 20)

	user, err := base.GetUserByCtx(c)
	if err != nil {
		user = &models_main.User{Id: 0}
	}

	resp, err := base.Service.GetLotteryHistory(user.Id, req)
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	return util.BuildSuccess(c, resp, "")
}
